// pages/dynamic/dynamicHome/dynamicHome.js
var app = getApp();
var _PAGE = 1;
var _PAGESIZE = 10;
var load= true;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    titleimg: "../../../images/install.png", // 订单动态图标
    is_request: 1, // 开关操作
    dynamic: [], // 服务动态数据
    Equipment: [], // 设备动态数据
    Order: [], // 订单动态数据
    isActive: 1, // 动态类型值
    empty: true,
    hasMore: false,
    loading: false,
    loadShow: false,
    changeload: false, // 数据加载状态
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    var that = this;
    var isActive = wx.getStorageSync('dynamicIndex') || 1;
    that.setData({
      isActive: isActive
    })
    _PAGE = 1;
    if (isActive == 1) {
      that.getDynamicList(); // 通知消息
    }
    if (isActive == 2) {
      that.getEquipmentDynamic();
      // 设备动态
    }
    if (isActive == 3) {
      that.getUserOrderDynamic(); // 订单动态
    }
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    var that = this;
    var isActive = that.data.isActive; // 类型值
    var hasMore = that.data.hasMore; // 开关值
    if (!hasMore) {
      return false;
    }
    _PAGE++;
    // 通知消息
    if (isActive == 1) {
      that.getDynamicList(); // 通知消息
    }
    if (isActive == 2) {
      that.getEquipmentDynamic(); // 设备动态
    }
    if (isActive == 3) {
      that.getUserOrderDynamic(); // 订单动态
    }
  },

  // 获取服务动态列表
  getDynamicList() {
    var that = this;
    that.setData({
      changeload: true
    })
    app.ajax({
      url: 'Common/Dynamic/dynamicList',
      data: {
        'type': 2,
        page: _PAGE,
        pageSize: _PAGESIZE
      },
      success: function(res) {
        if (res.data.code == 1000) {
          if (_PAGE == 1) {
            //console.log('操作', res.data.data[0].dynamicData[0].image)
            that.setData({
              dynamic: res.data.data,
              empty: false
            })
          } else {
           
            that.setData({
              dynamic: that.data.dynamic.concat(res.data.data),
              empty: false
            })
            
          }


          // 是否加载更多
          var hasMore = true;
          if (res.data.data.length < _PAGESIZE) {
            hasMore = false;
          }
          that.setData({
            hasMore: hasMore,
            loading: false
          });
          load=false;
        } else {
          if (_PAGE == 1) {
            that.setData({
              hasMore: false,
              loading: false,
              empty: true
            })
          } else {
            that.setData({
              hasMore: false,
              loading: false,
              empty: false
            })
          }
          // app.showToast(res.data.message);
        }
        that.setData({
          loadShow: true,
          changeload: false
        })
        
      }
    })
  },

  // 服务动态详情点击
  clickDetail: function(e) {
    console.log('动态ID', e.currentTarget.dataset.id);
    var that = this;
    var dynamic_id = e.currentTarget.dataset.id; // 获取动态ID
    wx.navigateTo({
      url: "../../dynamic/dynamicDetail/dynamicDetail?id=" + dynamic_id
    })
  },

  // 获取设备动态列表
  getEquipmentDynamic() {
    var that = this;
    that.setData({
      changeload: true
    })
    app.ajax({
      url: 'User/Equipmentlists/getEquipmentDynamic',
      data: {
        page: _PAGE,
        pageSize: _PAGESIZE
      },
      success: function(res) {
        if (res.data.code == 1000) {
          if (_PAGE == 1) {
            that.setData({
              Equipment: res.data.data,
              empty: false
            })
          } else {
            that.setData({
              Equipment: that.data.Equipment.concat(res.data.data),
              empty: false
            })
          }
          // 是否加载更多
          var hasMore = true;
          if (res.data.data.length < _PAGESIZE) {
            hasMore = false;
          }
          that.setData({
            hasMore: hasMore,
            loading: false
          });
          load= false;
        } else {
          if (_PAGE == 1) {
            that.setData({
              hasMore: false,
              loading: false,
              empty: true
            })
          } else {
            that.setData({
              hasMore: false,
              loading: false,
              empty: false
            })
          }
        }
        that.setData({
          loadShow: true,
          changeload: false
        })
      }
    })

  },

  // 获取订单动态
  getUserOrderDynamic() {
    var that = this;
    that.setData({
      changeload: true
    })
    app.ajax({
      url: 'User/Order/userOrderDynamic',
      data: {
        page: _PAGE,
        pageSize: _PAGESIZE
      },
      success: function(res) {
        if (res.data.code == 1000) {
          if (_PAGE == 1) {
            that.setData({
              Order: res.data.data,
              empty: false
            })
          } else {
            that.setData({
              Order: that.data.Order.concat(res.data.data),
              empty: false
            })
          }
          // 是否加载更多
          var hasMore = true;
          if (res.data.data.length < _PAGESIZE) {
            hasMore = false;
          }
          that.setData({
            hasMore: hasMore,
            loading: false
          });
          load= false;
        } else {
          if (_PAGE == 1) {
            that.setData({
              hasMore: false,
              loading: false,
              empty: true
            })
          } else {
            that.setData({
              hasMore: false,
              loading: false,
              empty: false
            })
          }
        }
        that.setData({
          loadShow: true,
          changeload: false
        })
      }
    })
  },

  // 订单动态详情点击
  orderClickDetail: function(e) {
    var that = this;
    var order_id = e.currentTarget.dataset.id; // 获取订单ID
    var dynamic_type = e.currentTarget.dataset.dynamic_type; //类型： 1，订单；2，工单
    if (dynamic_type == 1) {
      wx.navigateTo({
        url: '../../user/myOrderDetail/myOrderDetail?order_id=' + order_id,
      })
    } else if (dynamic_type == 2) {
      wx.navigateTo({
        url: '../../user/afterSaleDetail/afterSaleDetail?detail=' + order_id,
      })
    }

  },
  // 设备详情
  facilityDetail: function(e) {
    var that = this;
    var equipment_id = e.currentTarget.dataset.equipment_id;
    if (e.currentTarget.dataset.type==10){
      return false;
    }
    wx.navigateTo({
      url: '../../facilityDetail/facilityDetail?equipment_id=' + equipment_id,
    })
  },


  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {
    var that = this;
    that.setData({
      loadShow: false
    })


  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {
    wx.removeStorageSync('dynamicIndex');
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {},

  // 动态切换
  changeDynamic: function(e) {
    var that = this;
    // 重置页码数开启开关
    _PAGE = 1;
    that.setData({
      empty: true,
      hasMore: false,
      loading: false,
      loadShow: false,
      changeload: true
    })
    load= true;

    // 获取点击的类型值
    var isActive = e.currentTarget.dataset.type;
    wx.setStorageSync('dynamicIndex', isActive);
    // 服务动态
    if (isActive == 1) {
      that.getDynamicList(); // 获取服务动态列表
    }
    // 设备动态
    if (isActive == 2) {
      that.getEquipmentDynamic(); // 获取设备动态列表
    }
    // 订单动态
    if (isActive == 3) {
      that.getUserOrderDynamic(); // 获取订单动态列表
    }

    this.setData({
      isActive: e.currentTarget.dataset.type
    })
  },
  //图片加载错误
  dynamic_error(e) {
    var that = this,
      dynamic = that.data.dynamic,
      inds = e.currentTarget.dataset.index;
    dynamic[0].dynamicData[0].image = '../../../images/none.png'
    that.setData({
      dynamic: dynamic
    })
  },
  //图片加载错误
  images_error(e) {
    var that = this,
      dynamic = that.data.dynamic,
      inds = e.currentTarget.dataset.index,
      nowIndex = e.currentTarget.dataset.nowindex;
    dynamic[inds].dynamicData[nowIndex].image = '../../../images/none.png'
    that.setData({
      dynamic: dynamic
    })
  },
  //服务动态图片
  Equipment_error(e) {
    var that = this,
      Equipment = that.data.Equipment,
      nowIndex = e.currentTarget.dataset.nowindex;
    Equipment[nowIndex].main_pic = '../../../images/none.png'
    that.setData({
      Equipment: Equipment
    })
  },
  //图片加载错误
  Order_error(e) {
    var that = this,
      Order = that.data.Order,
      nowIndex = e.currentTarget.dataset.nowindex;
    Order[nowIndex].main_pic = '../../../images/none.png'
    that.setData({
      Order: Order
    })
  }
})